<!-- //app.vue -->
<script>
	export default {
		globalData: {
			api: "http://192.168.1.103:8090", //后端 api
			//request header cookie
			rhc: "",
			userId: "", // id String 实际为 long
			token: "" // token String
		},
		onLaunch: function() {
			console.log('初始化')
			 this.login();
		},
		methods: {
			//微信登录
			login() {
				const globalData = this.globalData;
				const userInfo = this.globalData.userInfo; //全局用户信息
				if (globalData.userId !== "") {
					console.log('用户信息已存在')
					return;
				}
				console.log('登录请求开始')
				 
				wx.login({
					success(res) {
						if (res.code) {
							//发起网络请求
							console.log('js_code获取成功！发送的js_code为' + res.code)
							 uni.request({
								url: globalData.api + '/wx/login', //请求后端的接口地址
								data: {
									js_code: res.code
								},
								success: (res) => { //如果访问接口成功就会进入success
									//保存登录凭证
									const result = res.data.data;
									//获取 响应头 set-token信息
									const cookieString = res.cookies[0];
									//提取 satoken 使用正则表达式匹配 satoken的值
									const match = cookieString.match(/satoken=([^;]*)/);
									// 提取并打印 satoken的值
									const satokenValue = match[1];
									console.log(satokenValue); // 输出: 63dda67f-6d09-45a1-917d-50e3d0f14414
									globalData.userId = result.userId;
									globalData.token = cookieString;
									globalData.rhc = 'satoken=' + satokenValue + ';userId=' + result.userId;
									console.log('satoken已保存')
								}
							})
						}
					}
				});
			},

		}
	}
</script>

<style>
	/*每个页面公共css */
	page {
	        display: flex;
	        flex-direction: column;
	        height: 100%;
	    }
</style>